import request from '@neu/biz-core/utils/request'
import { getToken } from '@neu/biz-core/utils/auth'
import axios from 'axios'

export const page = (params) => {
  return request({
    url: '/cmdb/schemas/page',
    params,
    method: 'get'
  })
}

export const groups = () => {
  return request({
    url: '/cmdb/schemas/groups',
    method: 'get'
  })
}

export const detail = (id) => {
  return request({
    url: '/cmdb/schemas/' + id,
    method: 'get'
  })
}

export const detailByCode = (code) => {
  return request({
    url: '/cmdb/schemas/code/' + code,
    method: 'get'
  })
}

export const update = (id, data) => {
  return request({
    url: '/cmdb/schemas/' + id,
    data: data,
    method: 'put'
  })
}

export const create = (data) => {
  return request({
    url: '/cmdb/schemas',
    data: data,
    method: 'post'
  })
}

export const remove = (id) => {
  return request({
    url: '/cmdb/schemas/' + id,
    method: 'delete'
  })
}

export const tree = () => {
  return request({
    url: '/cmdb/schemas/tree',
    method: 'get'
  })
}

export const pureTree = () => {
  return request({
    url: '/cmdb/schemas/tree/pure',
    method: 'get'
  })
}

export const updateAnyFiled = (schemaId, field, data) => {
  return request({
    url: '/cmdb/schemas/' + schemaId + '/' + field,
    data,
    method: 'put'
  })
}

export const getRelations = (schemaId) => {
  const url = '/cmdb/schemas/' + schemaId + '/relations'
  return () => {
    return request({
      url: url,
      method: 'get'
    })
  }
}

export const addRelation = (schemaId, data) => {
  return request({
    url: '/cmdb/schemas/' + schemaId + '/relations/add',
    data,
    method: 'put'
  })
}

export const removeRelation = (schemaId) => {
  const urlPrefix = '/cmdb/schemas/' + schemaId + '/relations/'
  return (relationId) => {
    const url = urlPrefix + relationId
    return request({
      url: url,
      method: 'delete'
    })
  }
}

export const getTags = (schemaId) => {
  const url = '/cmdb/schemas/' + schemaId + '/tags'
  return () => {
    return request({
      url: url,
      method: 'get'
    })
  }
}

export const getIcon = (schemaId) => {
  return request({
    url: '/cmdb/schemas/' + schemaId + '/icon',
    method: 'get'
  })
}

export const uploadIcon = (schemaId) => {
  return '/cmdb/schemas/' + schemaId + '/icon'
}

export const outline = () => {
  return request({
    url: '/cmdb/schemas/outline',
    method: 'get'
  })
}

export const getTopo = (schemaId) => {
  return request({
    url: '/cmdb/schemas/' + schemaId + '/topo',
    method: 'get'
  })
}

export const downloadSchemaTpl = (schemadefId) => {
  const url = '/cmdb/schemas/' + schemadefId + '/tpl'
  return new Promise((resolve, reject) => {
    axios.get(url, { responseType: 'blob', headers: { 'Authentication': getToken() }})
      .then(response => {
        resolve(response)
      })
      .catch(error => {
        reject(error)
      })
  })
}

export const countOfSchemas = () => {
  return request({
    url: '/cmdb/schemas/count',
    method: 'get'
  })
}
